using System;
namespace ch.ethz.ssh2.util
{

    /// <summary> </summary>
    /// <author>  Christian Plattner
    /// </author>
    /// <version>  2.50, 03/15/10
    /// </version>
    public class StringEncoder
    {
        public static byte[] GetBytes(System.String data)
        {
            try
            {
                //iso-8859-1 ISO8859_1
                return System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(data);
            }
            catch (System.IO.IOException e)
            {
                byte[] bytes = new byte[data.Length];

                for (int i = 0; i < data.Length; i++)
                {
                    char c = data[i];
                    if (c > 127)
                        bytes[i] = (byte)(0x3F); // ?
                    else
                        bytes[i] = (byte)c;
                }

                return bytes;
            }
        }

        public static System.String GetString(byte[] data)
        {
            return GetString(data, 0, data.Length);
        }

        public static System.String GetString(byte[] data, int off, int len)
        {
            try
            {
                //System.String tempStr;
                //tempStr = System.Text.Encoding.GetEncoding("iso-8859-1").GetString(data, off, len);
                //return new System.String(tempStr.ToCharArray(), off, len);
                return System.Text.Encoding.GetEncoding("iso-8859-1").GetString(data, off, len);
            }
            catch (System.IO.IOException e)
            {
                char[] chars = new char[len];

                for (int i = 0; i < len; i++)
                {
                    char c = (char)data[off + i];
                    if (c > 127)
                        c = '?';
                    chars[i] = c;
                }

                return new System.String(chars);
            }
        }
    }
}